<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件名称" prop="partName">
        <el-input
          v-model="queryParams.partName"
          placeholder="请输入部件名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="图纸图号" prop="drawingNumber">
        <el-input
          v-model="queryParams.drawingNumber"
          placeholder="请输入图纸图号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="dictProcessName">
        <el-select
          v-model="queryParams.dictProcessName"
          placeholder="请选择工序名称"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.CRAFT_PROCESS_NAME)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
            :disabled="
              dict.label == '下料' ||
              dict.label == '机加' ||
              dict.label == '油漆' ||
              dict.label == '布线' ||
              dict.label == '电柜'
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="setSearchParams(queryParams)">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="operateClick">
      <div>
        <el-button
          v-hasPermi="['config:wage-quotations-confirmed-task:export']"
          type="primary"
          :loading="exportLoading"
          @click="handleExport"
        >
          数据导出
        </el-button>
        <el-button
          v-hasPermi="['config:wage-quotations-confirmed-task:update']"
          type="primary"
          @click="openForm"
        >
          编辑
        </el-button>
      </div>
      <div class="titleSet">
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <el-table
      ref="multipleTableRef"
      :height="tableHeight"
      border
      v-loading="loading"
      :data="tableObject.tableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" />
      <el-table-column label="序号" align="center" type="index" fixed="left" width="80">
        <template #default="scope">
          <span v-if="!scope.row.unitPrice || scope.row.unitPrice <= 0" style="color: red">{{
            scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <template v-for="(item, index) in tableTitle">
        <el-table-column
          v-if="item.isTable && ![0].includes(index)"
          :key="index"
          :type="item.type"
          :min-width="item.width"
          :align="item.align"
          :label="item.label"
          :prop="item.field"
        >
          <template #default="scope">
            <div v-if="!scope.row.unitPrice || scope.row.unitPrice <= 0" style="color: red">
              <dict-tag
                v-if="item.dictType"
                :type="item.dictType"
                :value="scope.row[item.field]"
                style="color: red"
              />
              <div v-else-if="item.label.includes('图纸图号')">
                <el-button type="primary" link @click="jumpPdfFile(scope.row.drawingNumber)">{{
                  scope.row.drawingNumber
                }}</el-button>
              </div>
              <span v-else>{{ scope.row[item.field] }}</span>
            </div>
            <div v-else-if="item.label.includes('图纸图号')">
              <el-button type="primary" link @click="jumpPdfFile(scope.row.drawingNumber)">{{
                scope.row.drawingNumber
              }}</el-button>
            </div>
            <div v-else>
              <dict-tag v-if="item.dictType" :type="item.dictType" :value="scope.row[item.field]" />
              <span v-else>{{ scope.row[item.field] }}</span>
            </div>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <Pagination
      :total="tableObject.total"
      v-model:page="tableObject.currentPage"
      v-model:limit="tableObject.pageSize"
      @pagination="getLabelAll"
    />
  </ContentWrap>

  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="WageQuotationsConfirmedTask">
import { jumpPdfFile } from '@/utils/formCreate'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import * as WageQuotationsConfirmedTaskApi from '@/api/config/wageQuotationsConfirmedTask'
const { push } = useRouter()
const tableTitle = ref<CrudSchema[]>([]) // 表头
const loading = ref(true) // 列表的加载中
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productNumber: null,
  productName: null,
  drawingNumber: null,
  partName: null,
  dictProcessName: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const { tableObject, tableMethods } = useTable({
  getListApi: WageQuotationsConfirmedTaskApi.getWageQuotationsConfirmedTaskPage, // 分页接口
  defaultParams: queryParams
})
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

/** 查询列表 */
const getLabelAll = async () => {
  loading.value = true
  try {
    const data = await WageQuotationsConfirmedTaskApi.getWageQuotationsConfirmedTaskPage(
      queryParams
    )
    // total.value = data.total
    const { allSchemas } = useCrudSchemas(data.labels)
    tableTitle.value = allSchemas.tableColumns
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 修改操作 */
const openForm = () => {
  push('/config/config/salaryAssessment/quotationsConfirmedForm')
}

// 打开表头设置
const tableTitleSet = ref()
const openTableTitleSet = () => {
  let code = 'WageQuotationsConfirmedTask'
  tableTitleSet.value.open(tableTitle.value, code)
}

// 多选
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}

// 导出数据
const multipleTableRef = ref()
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = ''
  } else {
    params.ids = multipleSelection.value.map((item) => (item as any).id).join(',')
  }
  WageQuotationsConfirmedTaskApi.exportWageQuotationsConfirmedTask(params).then((res) => {
    download.excel(res, val.name)
    multipleTableRef.value!.clearSelection()
  })
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  await getLabelAll()
  getList()
})
</script>
<style lang="scss" scoped>
.operateClick {
  height: 50px;
  display: flex;
  justify-content: space-between;
  .titleSet {
    display: flex;
    justify-content: flex-end;
    // text-align: right;
  }
}

:deep(.el-select .el-input) {
  width: 100%;
}

.unitPrice {
  color: red;
}
</style>
